<template>
  <div class="app-container">

    <u-page title="核销记录">

      <u-filtered>
        <el-form :inline="true" :model="listQuery" class="search-form">
          <el-form-item label="操作类型" prop="type">
            <el-select v-model="listQuery.type" placeholder="请选择操作类型" clearable>
              <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="卡号" prop="cardno">
            <el-input v-model="listQuery.cardno" placeholder="请输入卡号" clearable style="width: 170px" />
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="listQuery.mobile" placeholder="请输入手机号" clearable style="width: 180px" maxlength="11" show-word-limit />
          </el-form-item>
          <el-form-item label="OPENID" prop="openid">
            <el-input v-model="listQuery.openid" placeholder="请输入OPENID" style="width: 250px" clearable />
          </el-form-item>
          <el-form-item label="订单号" prop="orderNo">
            <el-input v-model="listQuery.orderNo" placeholder="订单号" clearable />
          </el-form-item>
          <el-form-item label="通联订单号" prop="orderId">
            <el-input v-model="listQuery.orderId" placeholder="通联订单号" clearable />
          </el-form-item>
          <el-form-item label="通联流水号" prop="tranNo">
            <el-input v-model="listQuery.tranNo" placeholder="通联流水号" clearable />
          </el-form-item>
          <el-form-item label="劵号" prop="couponNo">
            <el-input v-model="listQuery.couponNo" placeholder="请输入劵号" clearable />
          </el-form-item>
          <el-form-item label="名称" prop="couponName">
            <el-input v-model="listQuery.couponName" placeholder="请输入名称" clearable />
          </el-form-item>
          <el-form-item label="模板ID" prop="templateId">
            <el-input v-model="listQuery.templateId" placeholder="通联模板ID" clearable />
          </el-form-item>
          <el-form-item label="收银员" prop="createUserName">
            <el-input v-model="listQuery.createUserName" placeholder="收银员名称" clearable />
          </el-form-item>
          <el-form-item label="门店" prop="shopName">
            <el-input v-model="listQuery.shopName" placeholder="门店" clearable />
          </el-form-item>
          <el-form-item label="交易时间" prop="creatDate">
            <u-date-time-picker v-model="creatDate" @change="changeDate" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="hdlFilter">搜索</el-button>
            <el-button type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
          </el-form-item>
        </el-form>
      </u-filtered>

      <u-table
        :data="list"
        :loading="listLoading"
        :options="listOptions"
        :columns="columns"
        :pagination.sync="listQuery"
        :fetch="hdlList"
        @selection-change="hdlSelectChange"
      />

    </u-page>

  </div>
</template>
<script>
import api from '@/api/market/coupon/consume/log'

export default {
  data() {
    return {
      api: api,
      // 表格
      columns: [
        {
          prop: 'type',
          label: '操作类型',
          width: '90',
          render: (h, params) => {
            const { type } = params.row
            return h('el-tag', { props: { effect: 'plain' }}, this.typeList.find(item => item.value === type).label)
          }
        },
        {
          prop: 'createDate',
          label: '交易时间',
          width: '160'
        },
        {
          prop: 'orderNo',
          label: '订单号',
          width: '200'
        },
        {
          prop: 'orderId',
          label: '通联订单号',
          width: '200'
        },
        {
          prop: 'tranNo',
          label: '通联流水号',
          width: '200'
        },
        {
          prop: 'couponNo',
          label: '劵号',
          width: '200'
        },
        {
          prop: 'couponName',
          label: '名称',
          width: '160'
        },
        {
          prop: 'templateId',
          label: '模板ID'
        },
        {
          prop: 'couponType',
          label: '券类型',
          width: '120',
          render: (h, params) => {
            const { couponTypeName } = params.row
            return h('el-tag', { props: { effect: 'plain' }}, couponTypeName)
          }
        },
        {
          prop: 'balance',
          label: '金额/次',
          render: (h, params) => {
            const { balance, couponType } = params.row
            const unit = couponType === '2' ? '次' : '元'
            return h('span', { style: { fontWeight: 'bold', color: '#4CAF50 !important' }}, balance + unit)
          }
        },
        {
          prop: 'openid',
          label: 'OPENID',
          width: '250'
        },
        {
          prop: 'createUserSysno',
          label: '收银员ID'
        },
        {
          prop: 'createUserName',
          label: '收银员名称',
          width: '150'
        },
        {
          prop: 'shopName',
          label: '门店',
          width: '150'
        }
      ],

      list: [],
      listLoading: false,
      listOptions: {
        mutiSelect: false, // boolean 是否多选
        stripe: true // boolean 斑马纹
      },
      listQuery: {
        pageNumber: 1,
        pageSize: 10,
        totalCount: 1,
        openid: '',
        mobile: '',
        cardno: '',
        templateId: '',
        orderNo: '',
        tranNo: '',
        couponNo: '',
        createdBy: '',
        beginTime: '',
        endTime: ''
      },
      creatDate: [],
      // 操作类型
      typeList: [{ value: '1', label: '核销' }, { value: '2', label: '作废' }, { value: '3', label: '反核销' }],
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑核销记录',
        create: '新增核销记录'
      },
      dataForm: {},
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: ['blur'] }
        ]
      },
      // 删除选中数据
      selectData: [],
      newForm: {
        disabled: true
      }
    }
  },
  methods: {
    // 时间范围
    changeDate(item) {
      if (item) {
        this.listQuery.beginTime = item[0]
        this.listQuery.endTime = item[1]
      } else {
        this.listQuery.beginTime = ''
        this.listQuery.endTime = ''
      }
    },
    // 导出数据
    handleDownload() {
      // 复制值
      const list = this.$u.deepClone(this.list)
      // 业务数据处理
      list.forEach(item => {
        this.$set(item, 'type', item.type === '1' ? '核销' : item.type === '2' ? '作废' : item.type === '3' ? '反核销' : '')
        this.$set(item, 'couponType', item.couponType === '0' ? '现金卡' : item.couponType === '1' ? '代金券' : item.couponType === '2' ? '计次券' : item.couponType === '3' ? '满减活动券' : '')
        this.$set(item, 'balance', item.couponType === '2' ? item.balance + '次' : item.balance + '元')
        this.$set(item, 'createdAt', item.createdAt ? this.$u.timeFormat(item.createdAt) : '')
      })
      // 表头&表数据
      const tHeader = this.columns.map(item => { return item['label'] })
      const filterVal = this.columns.map(item => { return item['prop'] })
      const data = list.map(v => filterVal.map(j => { return v[j] }))
      this.$u.exports.excel('核销记录', tHeader, data)
    }
  }
}
</script>
